package com.yimi.yinhepda.views.message;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import cn.jpush.android.api.JPushInterface;

import com.pda.ym.base.BaseActivity;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.message.MessageListAdapter;
import com.yimi.yinhepda.db.dao.MyMessageDao;
import com.yimi.yinhepda.db.dao.UserReadMessageDao;
import com.yimi.yinhepda.entity.message.MyMessageBean;
import com.pda.ym.utils.DateHelper;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

/**
 * 消息列表界面
 *
 * @author 000432
 * @date 2018/1/13 0013
 */
public class MessageListActivity extends BaseActivity {

    /**
     * 消息列表
     */
    private ListView mListView;
    /**
     * 返回按钮
     */
    private Button mBtnBack;
    private MessageListAdapter mListAdapter;
    private MyMessageDao myMessageDao;
    private UserReadMessageDao mUserReadMessageDao;
    private LinearLayout mNoMessageLayout;
    public static Handler handler;
    /**
     * 刷新消息界面tag
     */
    public static final int FLASH_MESSAGE_ACTIVITY = 101;

    @Override
    public String getPageName() {
        return "消息列表界面";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_list);

        mListView = (ListView) findViewById(R.id.message_listView);
        mBtnBack = (Button) findViewById(R.id.message_btn_back);
        mNoMessageLayout = (LinearLayout) findViewById(R.id.message_ll_no_message);

        mBtnBack.setOnClickListener(this);

        myMessageDao = new MyMessageDao();
        mUserReadMessageDao = new UserReadMessageDao();

        //删除三个月前的数据
        deleteThreeMonthData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setHandler();
        initData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.message_btn_back:
                finish();
                break;
            default:
                break;
        }
    }

    private void initData() {
        List<MyMessageBean> myMessageBeanList = null;
        myMessageBeanList = myMessageDao.findAll();
        if (myMessageBeanList == null || myMessageBeanList.size() == 0) {
            mNoMessageLayout.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.GONE);
            return;
        } else {
            mListView.setVisibility(View.VISIBLE);
            mNoMessageLayout.setVisibility(View.GONE);
        }
        // 倒序排列
        Collections.reverse(myMessageBeanList);

        mListAdapter = new MessageListAdapter(this, myMessageBeanList);
        mListView.setAdapter(mListAdapter);

        final List<MyMessageBean> finalMyMessageBeanList = myMessageBeanList;
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MyMessageBean bean = finalMyMessageBeanList.get(position);
                String notfyId = bean.getNotificationId();
                if (notfyId == null) {
                    showToast("消息ID为空！");
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString(MessageDetailActivity.MESSAGE_NOTIFICATION_ID, notfyId);
                Intent intent = new Intent(MessageListActivity.this, MessageDetailActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @SuppressLint("HandlerLeak")
    private void setHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case FLASH_MESSAGE_ACTIVITY:
                        initData();
                        break;
                    default:
                        break;
                }
            }
        };
    }

    /***
     * 删除三个月前的数据
     */
    public void deleteThreeMonthData() {
        String oldDate = DateHelper.getOldDateStr(-90);
        mUserReadMessageDao.deleteThreeMonthData(oldDate);
        List<MyMessageBean> oldLists = myMessageDao.deleteThreeMonthData(oldDate);
        if (oldLists != null) {
            for (MyMessageBean bean : oldLists) {
                JPushInterface.clearNotificationById(this, Integer.parseInt(bean.getNotificationId()));
                try {
                    myMessageDao.delete(bean);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
